<?php

namespace app\erp\model;

use think\Model;

class JOrderdb extends Model
{
    protected $connection = 'database.juncai_db';
    protected $pk = 'order_id';
    protected $name = 'order';
    protected $supplier_id = 594;
    public $param = [];

    public function editFahuoGys()
    {
        $this->save([
            'gys_fh_state' => 1,
            'order_state' => 3,
            'wuliu' => $this->param ['wuliu'],
            'wuliudanhao' => $this->param ['wuliudanhao'],
            'gys_fh_time' => time()
        ], [
            'order_no' => $this->param ['order_no']
        ]);
        // 日志
        $JOrderLogindb = new JOrderLogdb();
        $save_data = array(
            'type_index' => 4,
            'order_no' => $this->param ['order_no'],
            'member_id' => $this->supplier_id,
            'view_text' => '分系统供应商已发货',
            'post_json' => $this->param
        );
        $JOrderLogindb->save($save_data);
    }

    public function editShouhuoGys()
    {
        $log_text = '订单收货';
        $type_index = 10;
        // 设置收货物流
        $expressdb = new JExpressdb();
        $get_express = $expressdb->where('order_no', $this->param ['order_no'])->find();
        $save_express['express_id'] = $get_express['express_id'];
        //  $save_express['receipt_time'] = time();
        // $save_express['receipt_state'] = 2;
        $save_express['receipt_member_id'] = $this->supplier_id;
        $get_express->save($save_express);
        $get_express_info = $expressdb->find($get_express['express_id']);
        /**    // 设置购物车产品收货状态
         * $cartdb = new JCartdb();
         * $cartdb->param ['order_no'] = $this->param ['order_no'];
         * $cartdb->param ['field'] = 'receipt_state';
         * $cartdb->param ['value'] = 1;
         * $cartdb->param ['cart_ids'] = explode(',', $get_express_info ['cart_ids']);
         * $cartdb->param ['is_exchange'] = $get_express_info ['is_exchange'];
         * $cartdb->editCartState();
         * // 全部配送完成
         * $this->save([
         * 'receipt_state' => 2,
         * 'receipt_time' => time()
         * ], [
         * 'order_no' => $this->param ['order_no']
         * ]);
         * // 日志
         * $order_logdb = new JOrderLogdb ();
         * // 日志
         * $save_log = array(
         * 'type_index' => $type_index,
         * 'order_no' => $this->param ['order_no'],
         * 'member_id' => $this->supplier_id,
         * 'view_text' => $log_text,
         * 'post_json' => ''
         * );
         * $order_logdb->save($save_log);
         **/
        // 处理供应商收货
        $this->save([
            'gys_sh_state' => 1,
            'order_state' => 4,
            'gys_sh_time' => time()
        ], [
            'order_no' => $this->param ['order_no']
        ]);
        // 日志
        $order_logdb = new JOrderLogdb ();
        $save_log = array(
            'type_index' => 5,
            'order_no' => $this->param ['order_no'],
            'member_id' => $this->supplier_id,
            'view_text' => '分系统供应商已收货',
            'post_json' => $this->param
        );
        $order_logdb->save($save_log);
    }

    public function confirmOrder()
    {
        // 确认订单
        $this->param['confirm_time'] = time();
        $this->allowField(true)->save($this->param ['post_data'], [
            'order_no' => $this->param ['order_no']
        ]);

        // 日志
        $JOrderLogindb = new JOrderLogdb();
        $save_data = array(
            'type_index' => 0,
            'order_no' => $this->param ['order_no'],
            'member_id' => $this->supplier_id,
            'view_text' => '确认订单',
            'post_json' => $this->param ['post_data']
        );
        $JOrderLogindb->save($save_data);

        // 写推送信息
        $postNoticedb = new JPostNoticedb();
        $save_post['post_type'] = 28;
        $save_post['sku_order_no'] = $this->param['order_no'];
        $postNoticedb->save($save_post);
    }

    public function confirmOrderGys()
    {
        $get_order = $this->where('order_no', $this->param['order_no'])->find();
        $this->where('order_id', $get_order['order_id'])->update($this->param);

        $buildtpldb = new JBuildtpldb();
        $buildtpldb->saveAll($this->param['tpl']);

        // 日志流转
        $order_logdb = new JOrderLogdb();
        $save_log = array(
            'type_index' => 3,
            'order_no' => $this->param ['order_no'],
            'member_id' => $this->supplier_id,
            'view_text' => '已确认订单',
            'post_json' => $this->param
        );
        $order_logdb->save($save_log);
    }

    public function setHkdw()
    {
        $this->where('order_no', $this->param['order_no'])->update(['huikuandanwei' => $this->param['huikuandanwei']]);
    }

    public function setInvoice()
    {
        $invoicedb = new JInvoicedb();
        $get_invoice = $invoicedb->where('order_nos', $this->param['order_no'])->find();
        if ($get_invoice) {
            $field = $this->param['post_data']['field'];
            $value = $this->param['post_data']['value'];
            $save_data[$field] = $value;
            $get_invoice->save($save_data);
            // 写日志
            $order_log = new JOrderLogdb();
            $save_log = array(
                'order_no' => $this->param ['order_no'],
                'member_id' => $this->supplier_id,
                'view_text' => '修改发票信息',
                'post_json' => $this->param ['post_data']
            );
            $order_log->save($save_log);
        }
    }

    public function applyInvoice()
    {
        $cartdb = new JCartdb();
        $data = $this->param['save_data'];
        $get_cart_arr = $cartdb->where('order_no', $this->param['order_no'])->select();
        foreach ($data as $key => $val) {

            $cart_id = $val['cart_id'];
            foreach ($get_cart_arr as $k => $v) {
                if ($v['goods_sku'] == $val['goods_sku']) {
                    $cart_id = $v['cart_id'];
                    $get_cart = $v;
                }
            }
            //$get_cart = $cartdb->with(['goods'])->find($cart_id);
            /*
            $save_update['product_area'] = $val['product_area'];
            $save_update['shuiwu_id'] = $val['shuiwu_id'];
            $save_update['product_firm'] = $val['product_firm'];
            $save_update['invoice_model'] = $val['invoice_model'];
            $save_update['goods_unit'] = $val['goods_unit'];
            $save_update['apply_state'] = 2;
            $save_update['nopass_info'] = '';
            $save_update['diy_beizhu'] = $val['diy_beizhu'];*/

            $save_update['product_area'] = $val['product_area']?:$get_cart['goods']['product_area'];
            $save_update['shuiwu_id'] = $val['shuiwu_id']?:$get_cart['goods']['shuiwu_id'];
            $save_update['product_firm'] = $val['product_firm']?:$get_cart['goods']['product_firm'];
            $save_update['invoice_model'] = $val['invoice_model']?:$get_cart['goods']['invoice_model'];
            $save_update['goods_unit'] = $val['goods_unit']?:$get_cart['goods']['goods_unit'];
            $save_update['goods_keywords'] = $val['goods_keywords']?:$get_cart['goods']['goods_keywords'];
            $save_update['apply_state'] = 2;
            $save_update['nopass_info'] = '';

            $cartdb->where('cart_id', $cart_id)->update($save_update);
        }
        $save_apply['order_no'] = $this->param['order_no'];
        $save_apply['admin_id'] = $this->supplier_id;
        $save_apply['log'] = '申请电子发票';
        $orderApplyEinvoicedb = new JOrderApplyEinvoicedb();
        $orderApplyEinvoicedb->save($save_apply);
        $orderdb = new Jorderdb();
        $get_order = $orderdb->where('order_no', $this->param['order_no'])->find();
        if ($get_order) {
            $get_order->save(['apply_state' => 2, 'apply_time' => time(), 'is_change' => $this->param['is_change']]);
        }
    }

    public function backApplyInfo()
    {
        $cartdb = new JCartdb();
        $data = $this->param['save_data'];
        $get_cart = $cartdb->where('order_no', $this->param['order_no'])->select();
        foreach ($data as $key => $val) {
            $cart_id = $val['cart_id'];
            foreach ($get_cart as $k => $v) {
                if ($v['goods_sku'] == $val['goods_sku']) {
                    $cart_id = $v['cart_id'];
                }
            }
            $save_update['apply_state'] = 1;
            $save_update['product_area'] = '';
            $save_update['shuiwu_id'] = '';
            $save_update['product_firm'] = '';
            $save_update['invoice_model'] = '';
            $save_update['goods_unit'] = '';
            $save_update['goods_keywords'] = '';
            $save_update['nopass_info'] = '';
            $save_update['diy_beizhu'] = '';
            $cartdb->where('cart_id', $cart_id)->update($save_update);
        }
        $save_apply['order_no'] = $this->param['order_no'];
        $save_apply['admin_id'] = $this->supplier_id;
        $save_apply['log'] = '撤销申请电子发票';
        $orderApplyEinvoicedb = new JOrderApplyEinvoicedb();
        $orderApplyEinvoicedb->save($save_apply);
    }

    // 修改备注
    public function editRemark()
    {
        $get_order = $this->where('order_no', $this->param['order_no'])->find();
        if ($get_order) {
            $get_order->save($this->param ['post_data']);
        }
    }
}